package com.topc.platform.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.topc.base.model.PageParams;
import com.topc.base.model.PageResult;
import com.topc.base.utils.PageUtils;
import com.topc.base.utils.TimeUtils;
import com.topc.platform.mapper.SignInUserMidMapper;
import com.topc.platform.model.po.SignInUserMid;
import com.topc.platform.model.vo.SignInShowVo;
import com.topc.platform.service.ISignInInfoService;
import com.topc.platform.service.ISignInUserMidService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Gogoing
 */
@Slf4j
@Service
public class SignInUserMidServiceImpl extends ServiceImpl<SignInUserMidMapper, SignInUserMid> implements ISignInUserMidService {


    @Autowired
    private ISignInInfoService signInInfoService;

    @Override
    public PageResult<SignInUserMid> searchMidByPage(PageParams params, int isToday, String userNumber) {
        LambdaQueryWrapper<SignInUserMid> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.equal(isToday, 1)){
            queryWrapper.ge(SignInUserMid::getCreateTime, TimeUtils.getDayStart(LocalDateTime.now()))
                    .le(SignInUserMid::getCreateTime, TimeUtils.getDayEnd(LocalDateTime.now()));
        }
        queryWrapper.like(ObjectUtil.isNotEmpty(userNumber),SignInUserMid::getUserNumber,userNumber);
        Page page = this.page(params.page(), queryWrapper);
        return PageUtils.pageToResult(page);
    }


    @Override
    public List<Integer> getHistoryCount(String number){
        LambdaQueryWrapper<SignInUserMid> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SignInUserMid::getUserNumber,number)
                .orderByDesc(SignInUserMid::getCreateTime)
                .last("limit 7");
        List<SignInUserMid> list = this.list(queryWrapper);
        List<Integer> collect = list.stream().map(SignInUserMid::getLuckyCount).collect(Collectors.toList());
        return collect;
    }

    @Override
    public SignInShowVo getAction(String number) {
        LambdaQueryWrapper<SignInUserMid> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SignInUserMid::getUserNumber,number).ge(SignInUserMid::getCreateTime,TimeUtils.getDayStart(LocalDateTime.now()));
        SignInUserMid signInUserMid = this.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(signInUserMid)){
            SignInUserMid newSign = new SignInUserMid();
            int sum = 0;
            for (int i = 0; i < 4; i++) {
                sum +=RandomUtil.randomInt(0,4);
            }
            int luckyCount = sum/4;
            newSign.setLuckyCount(luckyCount);
            List<Long> idsByRandom = signInInfoService.getIdsByRandom();
            newSign.setSignInFirst(idsByRandom.get(0));
            newSign.setSignInSecond(idsByRandom.get(1));
            newSign.setSignInThird(idsByRandom.get(2));
            newSign.setSignInFourth(idsByRandom.get(3));
            signInUserMid = newSign;
        }
        SignInShowVo signInShowVo = new SignInShowVo();
        signInShowVo.setLuckyCount(signInUserMid.getLuckyCount());
        signInShowVo.setSignInFirst(signInInfoService.getById(signInUserMid.getSignInFirst()).getAction());
        signInShowVo.setSignInSecond(signInInfoService.getById(signInUserMid.getSignInSecond()).getAction());
        signInShowVo.setSignInThird(signInInfoService.getById(signInUserMid.getSignInThird()).getAction());
        signInShowVo.setSignInFourth(signInInfoService.getById(signInUserMid.getSignInFourth()).getAction());
        return signInShowVo;
    }
}
